package com.unknown.wificontrol.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.unknown.wificontrol.R;
import com.unknown.wificontrol.bean.FileBean;
import com.unknown.wificontrol.widget.CustomGridView;

import java.io.File;
import java.util.List;

public class FileAdapter extends BaseQuickAdapter<FileBean, BaseViewHolder> {
    public FileAdapter(@Nullable List<FileBean> data) {
        super(R.layout.layout_image_history_list_item, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, FileBean item) {
        helper.setText(R.id.tvText, item.name);
        CustomGridView cgvImage = helper.getView(R.id.cgvImgs);
        final List<FileBean> subLists = item.files;
        SubFileAdapter subFileAdapter = new SubFileAdapter(getContext(), subLists);
        cgvImage.setAdapter(subFileAdapter);
        cgvImage.setFocusable(false);

        cgvImage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FileBean bean = subLists.get(position);
                if (onFileViewListener != null) {
                    onFileViewListener.onFileView(bean);
                }
            }
        });
        cgvImage.setFocusableInTouchMode(false);
    }

    private class SubFileAdapter extends DataAdapter<FileBean> {

        public SubFileAdapter(Context mContext, List<FileBean> mList) {
            super(mContext, mList);
        }

        @Override
        protected View makeView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.layout_history_list_item, parent, false);
            }
            ImageView ivImg = convertView.findViewById(R.id.ivImg);
            FileBean bean = mList.get(position);
            Bitmap bitmap = BitmapFactory.decodeFile(bean.path);
            ivImg.setImageBitmap(bitmap);
            return convertView;
        }
    }

    private OnFileViewListener onFileViewListener = null;

    public void setOnFileViewListener(OnFileViewListener onFileViewListener) {
        this.onFileViewListener = onFileViewListener;
    }

    public interface OnFileViewListener {
        public void onFileView(FileBean bean);
    }
}
